<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Work_order extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');
		$this->load->library('zend');
        //load in folder Zend
        $this->zend->load('Zend/Barcode');

	}
	public function index() {
		$this->load->view('layouts/header', $this->variable->load_data($this->session->userdata('title')));
		$this->load->view($this->session->userdata('formain'), array(
			'result' 		=> $this->work_order_model->check_role($this->session->userdata('user_id')),
			'role_id_datus' => $this->work_order_model->get_role_id($this->session->userdata('user_id')),
			'oprtr' 		=> $this->work_order_model->get_operators(),
			'wo_type' 		=> $this->work_order_model->get_maintain_type()
		));
		$this->load->view('layouts/footer');
	}


	public function get_req_data(){
		$query = $this->work_order_model->get_req_datus($this->input->post('req_id'));
		echo json_encode($query);
	}

	public function insert_request(){
			$data = array(
				':p_request_num' 	=>	$this->input->post('request_num'),
				':p_trans_source'	=>  'WORK_ORDER_SRC',
				':p_trans_src_id'	=> 	$this->input->post('maintain_id'),
				':p_request_date' 	=>	$this->input->post('request_date'),
				':p_requested_by' 	=>	$this->input->post('request_by'),
				':p_approved_by' 	=>	$this->input->post('approve_by'),
				':p_request_status' =>	$this->input->post('request_stat'),
				':p_user_id' 		=>	$this->session->userdata('user_id')
			);

			$this->work_order_model->insert_req_data($data);
	}

	public function update_request(){
			$data = array(
				':p_request_id' 	=>	$this->input->post('req_id'),
				':p_request_date' 	=>	$this->input->post('request_date'),
				':p_request_by' 	=>	$this->input->post('request_by'),
				':p_request_stat' 	=>	$this->input->post('request_stat'),
				':p_user_id' 		=>	$this->session->userdata('user_id')
			);

			$this->work_order_model->update_req_data($data);
	}

	public function insert_req_content(){
		$data = array(
	 		':p_request_id'		=> $this->input->post('c_req_id'),
	 		':p_sequence_num'	=> $this->input->post('req_sequence'),
	 		':p_item_id'		=> $this->input->post('request_item'),
	 		':p_item_qty'		=> $this->input->post('request_qty'),
	 		':p_user_id'		=> $this->session->userdata('user_id')
	 		);
	 	$query= $this->work_order_model->insert_content($data);
	 	//if($query[0]->error == 1){
			echo json_encode($query);
		//}else{
		//	$this->view_req_contents($this->input->post('c_req_id'));
		//}
	}

	public function delete_req_contents(){
 		$data = array(
	 		':p_line_id'		=> $this->input->post('line_id')
	 		);
	 	$query = $this->work_order_model->delete_req_contents($data);
		//if($query[0]->error == 1){
			echo json_encode($query);
		//}else{
		//	$this->view_req_contents($this->input->post('request_id'));
		//}
 	}


	public function view_req_contents($id){
		$query = $this->work_order_model->get_contents($id);
		echo json_encode($query);
	}

	public function get_customers() {
		if ($this->input->post('wo_type') == "NEW") {
			$query = $this->work_order_model->get_pending_cus();
		} else if ($this->input->post('wo_type') == "REC") {
			$query = $this->work_order_model->get_inactive_cus();
		} else {
			$query = $this->work_order_model->get_active_cus();
		}
		echo json_encode($query);

	}
	// SUPERVISOR
	public function insert_work_order() {
		if ($this->input->post('wo_maintain_type') == 'REC') {
			$last_read = 0;
			foreach ($this->work_order_model->get_last_read($this->input->post('wo_acct_name')) as $reading) {
				$last_read = $reading->last_reading;
			}
			$data = array(
				':p_maintain_code' 		=> $this->input->post('wo_maintain_type'),
				':p_account_id' 		=> $this->input->post('wo_acct_name'),
				':p_status' 			=> 'Pending',
				':p_target_date' 		=> $this->input->post('wo_target_date'),
				':p_assigned_to' 		=> $this->input->post('wo_assign'),
				':p_last_meter_reading' => $last_read,
				':p_user_id' 			=> $this->session->userdata('user_id'),
			);
			$this->work_order_model->insert_work_order_rec($data);
		} else {
			$data = array(
				':p_maintain_code'	=> $this->input->post('wo_maintain_type'),
				':p_account_id'		=> $this->input->post('wo_acct_name'),
				':p_status' 	 	=> 'Pending',
				':p_target_date' 	=> $this->input->post('wo_target_date'),
				':p_assigned_to' 	=> $this->input->post('wo_assign'),
				':p_user_id' 		=> $this->session->userdata('user_id'),
			);
			$this->work_order_model->insert_work_order($data);
		}
	}

	public function update_work_order() {
		$data = array(
			':p_assigned_to' => $this->input->post('edit_wo_assign'),
			':p_target_date' => $this->input->post('edit_target_date'),
			':p_maintain_id' => $this->input->post('h_maintain_id'),
			':p_user_id' 	 => $this->session->userdata('user_id'),
		);
		$this->work_order_model->update_work_order($data);
	}

	public function update_work_order_comp() {
		if ($this->input->post('c_wo_code') == 'NEW' || $this->input->post('c_wo_code') == 'REC') {
			if($this->input->post('c_wo_code') == 'REC'){

				$data = array(
				':p_new_meter_reading'	=> $this->input->post('wo_new_reading'),
				':p_account_id' 		=> $this->input->post('c_acct'),
				':p_maintain_id' 		=> $this->input->post('h_c_maintain_id'),
				':p_user_id' 			=> $this->session->userdata('user_id'),
				':p_last_meter_reading' => $this->input->post('wo_last_reading')
				);

			}else{
			$data = array(
				':p_new_meter_reading'	=> $this->input->post('wo_new_reading'),
				':p_account_id' 		=> $this->input->post('c_acct'),
				':p_maintain_id'		=> $this->input->post('h_c_maintain_id'),
				':p_user_id' 			=> $this->session->userdata('user_id'),
				':p_last_meter_reading' => $this->input->post('wo_last_reading')
				);
			}
			$this->work_order_model->update_work_order_comp_new($data);
		}
		if ($this->input->post('c_wo_code') == 'REP') {
			$data = array(
				':p_new_meter_reading' 	=> $this->input->post('wo_new_reading'),
				':p_last_meter_reading' => $this->input->post('wo_last_reading'),
				':p_maintain_id' 		=> $this->input->post('h_c_maintain_id'),
				':p_user_id' 			=> $this->session->userdata('user_id'),
				':p_account_id' 		=> $this->input->post('c_acct'),
			);
			$this->work_order_model->update_work_order_comp_rep($data);
		}
		if ($this->input->post('c_wo_code') == 'DIS') {
			$data = array(
				':p_last_meter_reading' => $this->input->post('wo_last_reading'),
				':p_account_id' 		=> $this->input->post('c_acct'),
				':p_maintain_id'	 	=> $this->input->post('h_c_maintain_id'),
				':p_user_id' 			=> $this->session->userdata('user_id'),
			);
			$this->work_order_model->update_work_order_comp_dis($data);
		}

	}

	public function insert_dev() {
		$data = array(
			':p_item_id' 				=> $this->input->post('item_num'),
			':p_device_type' 			=> $this->input->post('dev_type'),
			':p_model_number' 			=> $this->input->post('model_number'),
			':p_serial_number' 			=> $this->input->post('serial_number'),
			':p_device_id' 				=> $this->input->post('dev_id'),
			':p_mmeter_id'				=> $this->input->post('ins_mom_meter'),
			':p_latitude'  				=> $this->input->post('devLat'),
			':p_longitude' 				=> $this->input->post('devLng'),
			':p_install_date' 			=> $this->input->post('install_date'),
			':p_warranty_period' 		=> $this->input->post('warranty_per'),
			':p_initial_reading' 		=> $this->input->post('initial_read'),
			':p_last_reading' 			=> $this->input->post('last_read'),
			':p_maintenance_cycle'		=> $this->input->post('maint_cycle'),
			':p_replacement_cycle' 		=> $this->input->post('repl_cycle'),
			':p_maintenance_cycle_unit'	=> $this->input->post('maint_cycle_unit'),
			':p_replacement_cycle_unit'	=> $this->input->post('repl_cycle_unit'),
			':p_status' 				=> $this->input->post('status'),
			':p_device_remarks' 		=> $this->input->post('dev_remarks'),
			':p_created_by' 			=> $this->session->userdata('user_id'),
			':p_updated_by' 			=> $this->session->userdata('user_id'),
			':p_account_id' 			=> $this->input->post('account_id'),
			':p_work_id' 				=> $this->input->post('maintain_id'),
		);
		//var_dump($data);
		$result = $this->work_order_model->insertDataDev($data);
		 if($result['err'] == 1){
		 	echo json_encode($result);
		 }else{
		 	$this->generate_barcode($this->input->post('account_id'),$this->input->post('serial_number'));
		 	echo json_encode($result);
		 }
	}

	public function insert_dev_edit() {
		$data = array(
			':p_item_id' 				=> $this->input->post('item_num'),
			':p_device_type' 			=> $this->input->post('dev_type'),
			':p_model_number' 			=> $this->input->post('model_number'),
			':p_serial_number' 			=> $this->input->post('serial_number'),
			':p_device_id' 				=> $this->input->post('dev_id'),
			':p_mmeter_id'				=> $this->input->post('mom_meter'),
			':p_latitude'  				=> $this->input->post('e_devLat'),
			':p_longitude' 				=> $this->input->post('e_devLng'),
			':p_install_date' 			=> $this->input->post('install_date'),
			':p_warranty_period' 		=> $this->input->post('warranty_per'),
			':p_initial_reading' 		=> $this->input->post('initial_read'),
			':p_last_reading' 			=> $this->input->post('last_read'),
			':p_maintenance_cycle'		=> $this->input->post('maint_cycle'),
			':p_replacement_cycle' 		=> $this->input->post('repl_cycle'),
			':p_maintenance_cycle_unit'	=> $this->input->post('maint_cycle_unit'),
			':p_replacement_cycle_unit'	=> $this->input->post('repl_cycle_unit'),
			':p_status' 				=> $this->input->post('status'),
			':p_device_remarks' 		=> $this->input->post('dev_remarks'),
			':p_created_by' 			=> $this->session->userdata('user_id'),
			':p_updated_by' 			=> $this->session->userdata('user_id'),
			':p_account_id' 			=> $this->input->post('account_id'),
			':p_work_id' 				=> $this->input->post('maintain_id'),
		);
		//var_dump($data);
		$result = $this->work_order_model->insertDataDev($data);
		 if($result['err'] == 1){
		 	echo json_encode($result);
		 }else{
		 	$this->generate_barcode($this->input->post('account_id'),$this->input->post('serial_number'));
		 	echo json_encode($result);
		 }
	}

	public function generate_barcode($id,$serial){

		$acct_num = $this->work_order_model->get_account_num($id);

		$imageResource = Zend_Barcode::factory('code128', 'image', array('text'=>$serial), array('imageType' => 'png'))->draw();
        imagepng($imageResource, 'assets/barcodes/'.$acct_num['acct_num'].'.png');
	}
	public function search_work_order() {
		$query = $this->work_order_model->search_work_order($this->input->post('search_wo_type'), $this->input->post('search_wo_acct'), $this->input->post('search_wo_stat'),$this->input->post('search_wo_assign'),$this->input->post('search_wo_tardate'),$this->input->post('search_wo_comdate'));

		if (!empty($query)) {
			echo json_encode($query);
		} else {
			echo json_encode("none");
		}

	}


	public function add_form() {
		$this->load->view($this->session->userdata('foradd'), array(
			'wo_type' 	=> $this->work_order_model->get_maintain_type(),
			'assgn' 	=> $this->work_order_model->get_operators(),
			'devtype' 	=> $this->work_order_model->get_dev_type(),
			'cycleunit' => $this->work_order_model->getcycleunit(),
			'devstats' 	=> $this->work_order_model->get_dev_stats(),
			'item' 		=> $this->work_order_model->get_item(),
			'mom_meter'	=> $this->work_order_model->get_mother_meter()
		));
	}
	public function edit_form($id, $role) {
		$q = $this->work_order_model->get_work_order_data($id);
		$ai;
		foreach ($q as $d) {
			$ai = $d->account_id;
		}
		$this->load->view($this->session->userdata('foredit'), array(
			'wo_type' 	=> $this->work_order_model->get_maintain_type(),
			'assgn' 	=> $this->work_order_model->get_operators(),
			'operation' => $role,
			'wo_data' 	=> $this->work_order_model->get_wo_data($id),
			'devtype' 	=> $this->work_order_model->get_dev_type(),
			'cycleunit' => $this->work_order_model->getcycleunit(),
			'devstats' 	=> $this->work_order_model->get_dev_stats(),
			'item' 		=> $this->work_order_model->get_item(),
			'device' 	=> $this->work_order_model->get_dev($ai),
			'mom_meter'	=> $this->work_order_model->get_mother_meter(),
			'items'		=> $this->work_order_model->get_items(),
			'req_num'	=> $this->work_order_model->get_reqNum('INV_REQUEST_NUM'),
			'users'		=> $this->work_order_model->get_inv_users($this->session->userdata('company_id'),'REQ'),
			'status'	=> $this->work_order_model->get_status()
		));
	}
	public function view_form($id) {
		$this->load->view($this->session->userdata('forview'), array(
			'wo_datus' 	=> $this->work_order_model->get_work_order_data($id),
			'devtype' 	=> $this->work_order_model->get_dev_type(),
			'cycleunit' => $this->work_order_model->getcycleunit(),
			'devstats' 	=> $this->work_order_model->get_dev_stats(),
			'item' 		=> $this->work_order_model->get_item(),
		));
	}

	public function get_dev() {
		echo json_encode($this->work_order_model->get_dev3($this->input->post('maintain_id'),$this->input->post('work_type')));
	}
	public function get_dev2() {
		echo json_encode($this->work_order_model->get_dev2($this->input->post('account_id'),$this->input->post('work_type')));
	}
	public function update_dev() {
		$data = array(
			':p_item_id' 				=> $this->input->post('item_num'),
			':p_device_type' 			=> $this->input->post('dev_type'),
			':p_model_number' 			=> $this->input->post('model_number'),
			':p_serial_number' 			=> $this->input->post('serial_number'),
			':p_device_id' 				=> $this->input->post('dev_id'),
			':p_mmeter_id'				=> $this->input->post('mom_meter'),
			':p_latitude'				=> $this->input->post('e_devLat'),
			':p_longitude'				=> $this->input->post('e_devLng'),
			':p_install_date' 			=> $this->input->post('install_date'),
			':p_warranty_period' 		=> $this->input->post('warranty_per'),
			':p_initial_reading' 		=> $this->input->post('initial_read'),
			':p_last_reading' 			=> $this->input->post('last_read'),
			':p_maintenance_cycle' 		=> $this->input->post('maint_cycle'),
			':p_replacement_cycle' 		=> $this->input->post('repl_cycle'),
			':p_maintenance_cycle_unit' => $this->input->post('maint_cycle_unit'),
			':p_replacement_cycle_unit' => $this->input->post('repl_cycle_unit'),
			':p_status' 				=> $this->input->post('status'),
			':p_device_remarks' 		=> $this->input->post('dev_remarks'),
			':p_updated_by' 			=> $this->session->userdata('user_id'),
			':p_account_id' 			=> $this->input->post('account_id'),
			':p_device_assignment_id' 	=> $this->input->post('device_ass_id'),
		);
		//var_dump($data);
		$result = $this->work_order_model->updateDataDev($data);
		if($result['err'] == 1){
		 	echo json_encode($result);
		 }else{
		 	$this->generate_barcode($this->input->post('account_id'),$this->input->post('serial_number'));
		 	echo json_encode($result);
		 }
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */